package com.efamily.ewallet.server;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

import com.efamily.ewallet.client.account.bean.AccountBean;
import com.efamily.ewallet.client.account.bean.AccountSearchConfig;
import com.efamily.ewallet.client.account.bean.AccountBean.Gender;
import com.efamily.ewallet.client.account.service.AccountManagerService;
import com.efamily.ewallet.client.common.bean.UserBean;
import com.efamily.ewallet.client.exception.WrongAccountNameException;
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class AccountManagerServiceImpl extends RemoteServiceServlet implements
		AccountManagerService {

	public List<String> getCountriesList() {
		List<String> countriesList = new ArrayList<String>();
		List<Locale> locales = Arrays.asList(Locale.getAvailableLocales());
		Collections.sort(locales, new Comparator<Locale>() {

			public int compare(Locale o1, Locale o2) {
				final String contry1 = o1.getDisplayCountry(Locale.ENGLISH);
				final String contry2 = o2.getDisplayCountry(Locale.ENGLISH);
				return contry1.compareToIgnoreCase(contry2);
			}

		});
		for (Locale locale : Locale.getAvailableLocales()) {
			final String contry = locale.getDisplayCountry(Locale.ENGLISH);
			if (contry.length() > 0) {
				System.out.println("Country = " + contry + ". ISO code: "
						+ locale.getISO3Country());
				countriesList.add(contry);
			}
		}
		Collections.sort(countriesList);
		return countriesList;
	}

	public void createAccount(AccountBean accountBean, String password, String mail)
			throws WrongAccountNameException {
		// check account name/ throw exception if needed
	}

	public boolean verifyNickName(String nickName) {
		return true;
	}

	public PagingLoadResult<AccountBean> getPublicAccountList(
			AccountSearchConfig searchConfig, PagingLoadConfig config) {
		List<AccountBean> beans = loadAccountBeans();
		if (config.getSortInfo().getSortField() != null) {
			final String sortField = config.getSortInfo().getSortField();
			if (sortField != null) {
				Collections.sort(beans, config.getSortInfo().getSortDir()
						.comparator(new Comparator<AccountBean>() {
							public int compare(AccountBean o1, AccountBean o2) {
								if (sortField.equals("id")) {
									return o1.getId() < o2.getId() ? -1 : (o1
											.getId() == o2.getId()) ? 0 : 1;
								} else if (sortField.equals("nickName")) {
									return o1.getNickName().compareTo(
											o2.getNickName());
								} else if (sortField.equals("gender")) {
									return o1.getGender().compareTo(o2.getGender());
								} else if (sortField.equals("age")) {
									return o1.getAge() < o2.getAge() ? -1 : (o1
											.getAge() == o2.getAge()) ? 0 : 1;
								} else if (sortField.equals("occupation")) {
									return o1.getOccupation().compareTo(
											o2.getOccupation());
								} else if (sortField.equals("country")) {
									return o1.getCountry().compareTo(
											o2.getCountry());
								} else if (sortField.equals("balance")) {
									return o1.getBalance() < o2.getBalance() ? -1
											: (o1.getBalance() == o2
													.getBalance()) ? 0 : 1;
								}
								return 0;
							}
						}));
			}
		}

		List<AccountBean> sublist = new ArrayList<AccountBean>();
		int start = config.getOffset();
		int limit = beans.size();
		if (config.getLimit() > 0) {
			limit = Math.min(start + config.getLimit(), limit);
		}
		for (int i = config.getOffset(); i < limit; i++) {
			sublist.add(beans.get(i));
		}
		return new BasePagingLoadResult<AccountBean>(sublist, config
				.getOffset(), beans.size());

	}

	public boolean deleteAccounts(List<Integer> accountsID) {
		// TODO Auto-generated method stub
		return true;
	}
	
	private List<AccountBean> loadAccountBeans() {
		List<AccountBean> beans = new ArrayList<AccountBean>();

		AccountBean bean1 = new AccountBean(1, "McDuck, Skrudzh", Gender.MALE, 54,
				"Businessman", "USA", 279237345);
		beans.add(bean1);
		AccountBean bean2 = new AccountBean(2, "O. Twist", Gender.MALE, 12,
				"Unemployed", "Great britain", 3.75);
		beans.add(bean2);
		AccountBean bean3 = new AccountBean(3, "Gobsek", Gender.MALE, 67,
				"Money-keeper", "France", 300000.13);
		beans.add(bean3);

		return beans;
	}
}
